﻿/**
 * ...
 * @author sminutoli
 */
import com.dmotiko.meip.MainBtn;
import com.dmotiko.prune.*
import com.dmotiko.general.*
import mx.data.types.Obj;
import mx.transitions.easing.None;
import mx.transitions.easing.Regular;
import mx.transitions.Tween;
import mx.utils.Delegate;

class SoundSection extends Section
{
	
	private var mcController:MovieClip;
	private var mcPlay:ButtonClip;
	private var mcStop:ButtonClip;
	private var bPaused:Boolean;
	private var music:Sound;
	private var oSound:Object;
	private var nLastPosition:Number;
	private var musicCheck:MovieClip;
	
	private var mcContact:MainBtn;
	private var mcBelong:MainBtn;
	private var mcFAQ:MainBtn;
	private var mcGift:MainBtn;
	private var mcBack:MainBtn;
	private var btn_listener:Function;
	private var sound_listener:Function;
	private var sound_btn_listener:Function;
	private var section_listener:Function;
	
	private function initClip():Void {
		super.initClip();
		_alpha = 0;
		_visible = false;
		
		oSound = new Object();
		oSound.volume = 0;
		
		nLastPosition = 0;
		
		var root = this;
		
		var sMusic = "music.mp3";
		if ( _root["music_src"] ) sMusic = _root["music_src"];
		
		music = new Sound();
		music.loadSound(sMusic);
		music.onLoad = function() {
			this.setVolume(0);
			//Site.getApp().log( "SoundSection | music.onLoad" );
			Site.getApp().setSound(true);
		}
						
		music.onSoundComplete = function() {
			//Site.getApp().log( "SoundSection | music.onSoundComplete" );
			this.start(0);
		}
		
		sound_listener = Delegate.create( this, sound_changed );
		sound_btn_listener = Delegate.create(this, toggle_snd);
		btn_listener = Delegate.create(this, btn_changed);
		section_listener = Delegate.create(this, section_changed);
		
		_x = Stage.width - _width;
		_y = Stage.height - _height - 10;
		
		Site.getApp().addEventListener( WebSite.SOUND_CHANGED, sound_listener );
		Site.getApp().addEventListener( WebSite.SECTION_CHANGED, section_listener );
	}
	
	private function section_changed(e):Void 
	{
		var nY;
		if ( e.section == SiteSections.STORES ) {
			nY = Stage.height - _height;
		} else {
			nY = Stage.height - _height - 10;
		}
		registerTween( "moveY", new Tween( this, "_y", Regular.easeOut, this._y, nY, 0.5, true) );
		
		if ( e.section == SiteSections.CONTACT ) {
			if ( mcContact._currentframe == 1 ) mcContact.onRollOver();
			mcContact.setActive( true, true );
		} else {
			mcContact.setActive( false, true );
		}
		
		if ( e.section == SiteSections.FASHION ) {
			mcBack.gotoAndPlay("showFashion");
			Site.getApp().setSound(false);
			
		} else if( mcBack._currentframe > 1 ) {
			mcBack.gotoAndPlay("hideFashion");
			Site.getApp().setSound(true);
			
		}
	}
	
	function childLoaded( e ):Void {
		
		if ( e.target == mcPlay ) {
			mcPlay.addEventListener( BaseClipEvent.CHANGED, sound_btn_listener );
						
		} else if ( e.target == mcStop) {
			mcStop.addEventListener( BaseClipEvent.CHANGED, sound_btn_listener );
			
		} else if ( e.target == mcContact) {
			mcContact.setData( { label: "CONTACTO", value: SiteSections.CONTACT } );
			mcContact.addEventListener( BaseClipEvent.CHANGED, btn_listener);
			
		} else if ( e.target == mcBelong) {
			mcBelong.setData( { label: "PERTENECER", value: SiteSections.BELONG } );
			mcBelong.addEventListener( BaseClipEvent.CHANGED, btn_listener);
			
		} else if ( e.target == mcGift) {
			mcGift.setData( { label: "GIFT CARD", value: SiteSections.GIFT } );
			mcGift.addEventListener( BaseClipEvent.CHANGED, btn_listener);
			
		} else if ( e.target == mcFAQ) {
			mcFAQ.setData( { label: "FAQS", value: SiteSections.FAQ } );
			mcFAQ.addEventListener( BaseClipEvent.CHANGED, btn_listener);
			
		}
	}
	
	private function btn_changed( e ):Void 
	{
		Site.getApp().setSection( e.data.value );
	}
	
	private function toggle_snd( e ):Void {
		if ( e.target == mcPlay) {
			Site.getApp().setSound(true);
			
		} else {
			Site.getApp().setSound(false);
			
		}
	}
	
	private function sound_changed(e):Void 
	{
		if ( Site.getApp().getSound() ) {
			mcStop.setActive(false, true);
			mcPlay.onRollOver();
			mcPlay.setActive(true, true);
			pauseMusic(false);
			
		} else {
			mcPlay.setActive(false, true);
			mcStop.onRollOver();
			mcStop.setActive(true, true);
			pauseMusic(true);
			
		}
	}
	
	public function pauseMusic(b:Boolean, watch:Object):Void {
		bPaused = b;
		if (b) {
			registerTween( "fadeSound", new Tween( oSound, "volume", None.easeOut, oSound.volume, 0, 2, true), false, true, true);
			
		} else {
			music.start( nLastPosition / 1000 );
			registerTween( "fadeSound", new Tween( oSound, "volume", None.easeOut, oSound.volume, 100, 2, true), false, true, true);
		}		
	}
	
	private function tweenChanged(key, tween):Void {
		music.setVolume( oSound.volume );
	}
	private function tweenFinished(key, tween):Void {
		if ( oSound.volume == 0 ) {
			nLastPosition = music.position;
			music.stop();
		}
	}
	
}